<?php

Yii::import('common.models._base.BasePromotion');

class Promotion extends BasePromotion {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function attributeLabels() {
        return array(
            'id' => Yii::t('app', 'ID'),
            'promotion_name' => Yii::t('app', 'Promotion Name'),
            'discount_inpercentage' => Yii::t('app', 'In-Percentage'),
            'discount_inprice' => Yii::t('app', 'In-Price'),
            'start_date' => Yii::t('app', 'Start Date'),
            'end_date' => Yii::t('app', 'End Date'),
            'valid_date' => Yii::t('app', 'Valid Date'),
            'status' => Yii::t('app', 'Status'),
        );
    }

    /* this function use for auto generation coupon */

    public static function promotioncoupon($promotionType, $userId) {
        $currentDate = date('Y-m-d');
        $promotionValidation = Promotion::model()->findByPk($promotionType);
        $startDate = $promotionValidation->start_date;
        $endDate = $promotionValidation->end_date;
        if ($startDate <= $currentDate && $endDate >= $currentDate) {
            $model = new Coupons;
            $couponName = guid();
            $ClassCourseCategoryUsersRestrictionCoupon = new ClassCourseCategoryUsersRestrictionCoupon;
            $model->setAttribute('name', $couponName);
            $model->setAttribute('description', guid());
            $model->setAttribute('inprice', $promotionValidation->discount_inprice);
            $model->setAttribute('inpercentage', $promotionValidation->discount_inpercentage);
            $model->setAttribute('apply_discount_to', SkillConfig::STATUS_ACTIVE);
            if ($promotionValidation->valid_date != '') {
                $model->setAttribute('restrict_by_date', SkillConfig::STATUS_ACTIVE);
                $model->setAttribute('valid_from_date', $startDate);
                $model->setAttribute('valid_to_date', $endDate);
            }
            $model->setAttribute('maximum_number_of_redemptions_per_code', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('maximum_number_of_redemptions_per_user', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('maximum_number_of_prodcuts_to_discount', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('require_all_products_to_match', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('user_restriction', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('order_limit', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('minimum_order', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('maximum_order', SkillConfig::STATUS_DEACTIVE);
            $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('promotion_coupon', SkillConfig::STATUS_ACTIVE);
            $model->setAttribute('created', $currentDate);
            $model->save(false);
            $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('coupons_id', $model->id);
            $ClassCourseCategoryUsersRestrictionCoupon->setAttribute('classes_id', SkillConfig::STATUS_DEACTIVE);
            $ClassCourseCategoryUsersRestrictionCoupon->save(false);
            Yii::app()->runController('Users/sendMailtouserpromotion/couponName/'.$model->name.'/username/'.$userId);
           
        }
    }

}